package utils
{
import flash.display.Sprite;
import flash.events.KeyboardEvent;

import mx.collections.IList;
import mx.controls.CheckBox;
import mx.controls.DataGrid;
import mx.controls.listClasses.IListItemRenderer;
import mx.core.mx_internal;

use namespace mx_internal;
/** 
 *  DataGrid that uses checkboxes for multiple selection
 * Demo
 * private var arr:Array = [
			{ firstName: "Alex", lastName: "Harui" },
			{ firstName: "Gordon", lastName: "Smith" },
			{ firstName: "Deepa", lastName: "Subramanian" },
			{ firstName: "Matt", lastName: "Chotin" },
			{ firstName: "Ely", lastName: "Greenfield" },
			{ firstName: "Kevin", lastName: "Lynch" },
			{ firstName: "Shantanu", lastName: "Narayan" },
			{ firstName: "Joan", lastName: "Lafferty" },
			{ firstName: "Ryan", lastName: "Frishberg" },
			{ firstName: "Jason", lastName: "Szeto" },
			{ firstName: "Mark", lastName: "Anders" },
			{ firstName: "Peter", lastName: "Flynn" },
			{ firstName: "Heidi", lastName: "Williams" }

		];
 *
 * <xmlns:local="com.threeti.ecargo.components.checkBoxDataGrid.*" />
 * <local:CheckBoxDataGrid id="dg" dataProvider="{arr}" 
					allowMultipleSelection="true"  >
		<local:columns>
			<mx:DataGridColumn dataField="firstName" headerText=" " width="20" 
			 sortable="false" 
			itemRenderer="com.threeti.ecargo.components.checkBoxDataGrid.CheckBoxRenderer" > 
			</mx:DataGridColumn>
		</local:columns>
	</local:CheckBoxDataGrid>
   如果要加全选功能 只要设置headerRenderer 
   headerRenderer="com.threeti.ecargo.components.CheckBoxHeaderRenderer"
 */
public class CheckBoxDataGrid extends DataGrid
{

    
	override protected function selectItem(item:IListItemRenderer,
                                  shiftKey:Boolean, ctrlKey:Boolean,
                                  transition:Boolean = true):Boolean
	{
		// only run selection code if a checkbox was hit and always
		// pretend we're using ctrl selection
		if (item is CheckBox)
			return super.selectItem(item, false, true, transition);
	    else 
	        return super.selectItem(item,shiftKey,ctrlKey,transition);
		//return false;
	}

	// turn off selection indicator
    override protected function drawSelectionIndicator(
                                indicator:Sprite, x:Number, y:Number,
                                width:Number, height:Number, color:uint,
                                itemRenderer:IListItemRenderer):void
    {
    	super.drawSelectionIndicator(indicator,x,y,width,height,color,itemRenderer);
    	
	}

	// whenever we draw the renderer, make sure we re-eval the checked state
    override protected function drawItem(item:IListItemRenderer,
                                selected:Boolean = false,
                                highlighted:Boolean = false,
                                caret:Boolean = false,
                                transition:Boolean = false):void
    {
    	if(item is CheckBox){
		CheckBox(item).invalidateProperties();
		super.drawItem(item, selected, highlighted, caret, transition);
    	}
	}

	// fake all keyboard interaction as if it had the ctrl key down
	override protected function keyDownHandler(event:KeyboardEvent):void
	{
		// this is technically illegal, but works
		event.ctrlKey = true;
		event.shiftKey = false;
		super.keyDownHandler(event);
	}
	
	
	//获取当前选择的项目，如果有多选，返回最前选择的那个项目
     public function get selectedTopItem():Object
    {
    	//如果选择多项 
    	if(this.selectedItems && this.selectedItems.length > 0){
	       var topIndex:int = int.MAX_VALUE ;
	       var view:IList = IList(collectionIterator.view);
	       var top:int = -1 ;
	       for each(var item:Object in this.selectedItems){
				top = 	view.getItemIndex(item);
				if(top != -1 && top < topIndex)
				   topIndex = top ;
	       }
			if(topIndex != int.MAX_VALUE){
				 
				 return view.getItemAt(topIndex);
			}
			return null ; 
	    }
    	else 
    	return  super.selectedItem ;
         
    } 

}

}